Win32 Regular DLL

Introduction

A Dynamic Link Library (DLL) is a program that holds one or more functions or some functionality that other programs can use (that's the shortest definition I could come up with...). As such, you can create one DLL and use it in many programs or even distribute it to other programmers (because a DLL is usually treated as an "executable" program, you cannot e-mail it to somebody as an attachment; most e-mail programs would not carry it). Most of the time, a DLL is just a file that has the extension .dll but it could also have another extension.

The DLL we are going to create can be used in any program that uses MFC. Unfortunately, and based on my experience, you should not distribute it to people who are using other compilers such as Borland C++ Builder or Dev-C++. In reality you can. Because they will not be able to simply "plug" it in their programs, apparently there are some things they can do to make it work. I cannot cover that. In fact, the only reason I am writing this is because I can't believe how hard it is to find this information, whether in books or on the Internet; people who write books either assume that everybody knows how to create a DLL or they simply don't know, which is incredible considering, as you will see soon, how easy this thing is (I myself looked for it in many places, to no avail).

Creating a Project DLL

To start, we need a project that would carry the DLL.

  1. Start Microsoft Visual C++. On the main menu, click File -> new...
  2. In the New dialog box, click the Projects tab and click Win32 Dynamic Link Library.
  3. In the Project Name, type a name such as ExoDLL and specify your desired folder in the Location box.
  4. Click OK
  5. In the Win32 Dynamic Link Library Step 1 of 1 wizard, click A Simple DLL Project (you could leave the default radio button but you would have to write your own entry function; I personally don't see why bother, if MSVC can write it for you, let it do it).
  6. Click Finish.
  7. Click OK.
  8. In the Workspace, click the + on ExoDLL Classes to expand.
  9. Click the + on Global to expand.
  10. Double-click DLLMain to open the file that holds the entry function.

Coding the DLL

To make this project a little useful for our learning process, we will use two types of functions: those that are used internally by the DLL only, and those that can be used by external functions, that is, the other programs that will need this DLL.

  1. To create functions that are used internally by the DLL, change the program as follows:
     
    // ExoDLL.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    
    // This function is used to calculate the total area of a parallelepid rectangle
    double BoxArea(double L, double H, double W);
    // This function is used to calculate the volume of a parallelepid rectangle
    double BoxVolume(double L, double H, double W);
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        return TRUE;
    }
    
    double BoxArea(double L, double H, double W)
    {
        return 2 * ((L*H) + (L*W) + (H*W));
    }
    
    double BoxVolume(double L, double H, double W)
    {
        return L * H * W;
    }
  2. Notice that the BoxArea() and the BoxVolume() functions are declared and defined just like any other function we know already.
    To declare and define a function that can be used outside of the DLL, that is, by a function that uses the DLL, change the file as follows:
     
    // ExoDLL.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    
    // This function is used to calculate the total area of a parallelepiped rectangle
    double BoxArea(double L, double H, double W);
    // This function is used to calculate the volume of a parallelepiped rectangle
    double BoxVolume(double L, double H, double W);
    // This function is used to get the dimensions of a rectangular parallelepiped
    // calculate the area and volume, then pass the calculated values to the
    // function that called it.
    extern "C" __declspec(dllexport)void BoxProperties(double Length, double Height,
                                        double Width, double& Area, double& Volume);
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        return TRUE;
    }
    
    double BoxArea(double L, double H, double W)
    {
        return 2 * ((L*H) + (L*W) + (H*W));
    }
    
    double BoxVolume(double L, double H, double W)
    {
        return L * H * W;
    }
    
    void BoxProperties(double L, double H, double W, double& A, double& V)
    {
        A = BoxArea(L, H, W);
        V = BoxVolume(L, H, W);
    }
  3. To save the project, on the main menu, click File -> Save All.
  4. To create the DLL, on the main menu, click Build -> Build ExoDLL.dll. At the end, you should receive ExoDLL.dll - 0 error(s), 0 warning(s)

Viewing the DLL

At this time, the DLL is ready. You can distribute or even sell it (to me).

Probably the easiest way to use a DLL you have created is to create a program in the same folder and test it. Nahhh, too easy. To make it more fun, we will use our DLL in a separate program on another folder. the technique we will use is the same you would use if you want to create a program in the same folder the DLL project was created.

  1. To see the DLL, open Windows Explorer or My Computer.
  2. Locate the folder where the DLL project was created.
  3. Inside the folder, display the content of the Debug folder. Notice two files: one has the extension .dll and another has .lib.
  4. Click the ExoDLL.dll file to select it.
  5. Press and hold Ctrl. then click ExoDLL.lib file to select both
  6. Release Ctrl
     
  7. On the main menu of Windows Explorer or My Computer, click Edit -> Copy

Using the DLL in a Console Application

The DLL we have created can be used in a Console application or an MFC program. We will start with a console application.

  1. To start a new C++ program, on the main menu of MSVC, click File -> New, click the Projects property sheet.
  2. Click Win32 Console Application. In the Project Name, type a name, such as ExoUsingDLL
  3. Click OK
  4. In the Win32 Console Application Step 1 of 1 wizard, accept the An Empty Project radio button and click Finish.
  5. Go back to Windows Explorer or My Computer.
  6. Locate the folder that has the current exercise. For me, that would be the ExoUsingDLL folder.
  7. Right-click in the folder and click Paste.
    The previously copied files should be pasted now in the new project folder.
  8. To create a new source file, on the main menu of MSVC, click File -> New...
  9. In the Files property sheet of the New dialog box, click C++ Source File. In the File Name edit box, type a name such as Exo and click OK
  10. To use the DLL, in the empty file, type the following:
     
    #include <iostream.h>
    
    extern "C" __declspec(dllimport)void BoxProperties(double L, double H,
                                        double W, double& A, double& V);
    
    int main()
    {
        double Length, Height, Width, Area, Volume;
    
        cout << "Enter the dimensions of the box\n";
        cout << "Length: ";
        cin >> Length;
        cout << "Height: ";
        cin >> Height;
        cout << "Width: ";
        cin >> Width;
    
        BoxProperties(Length, Height, Width, Area, Volume);
    
        cout << "\nProperties of the box";
        cout << "\nLength:  " << Length;
        cout << "\nHeight:  " << Height;
        cout << "\nWidth:   " << Width;
        cout << "\nArea:    " << Area;
        cout << "\nVolumne: " << Volume;
    
        cout << "\n\n";
    
        return 0;
    }
  11. To make the function of the DLL as part of the current project, on the main menu, click Project -> Add To Project -> Files...
  12. On the Insert Files Into Project dialog box, change the Files Of Type combo box to Library Files (*.lib).
  13. In the list of files, click ExoDLL.lib:
     
  14. Click OK
  15. To execute the program, press Ctrl + F5
    Your program should work fine.

Using the DLL in an MFC Application

We will proceed with the same ease to use our DLL in an MFC application.

  1. To create a new project, on the main menu of MSVC, click File -> New...
  2. From the Projects property sheet, click MFC AppWizard (exe). In the Project Name, type a name such as ExoMFCUsingDLL. Click OK
  3. In the MFC AppWizard Step 1, click Dialog Based and click Finish
  4. Open Windows explorer or My Computer. Locate the folder where the ExoDLL project was created and copy the ExoDLL.dll and ExoDLL.lib files.
  5. Paste them in the ExoMFCUsingDLL folder.
  6. Return to MSVC and design the dialog box as follows:
     
  7. For this exercise, name the edit controls as follows, from top to bottom, IDC_EDIT_LENGTH, IDC_EDIT_HEIGHT, IDC_EDIT_WIDTH, IDC_EDIT_AREA, and IDC_EDIT_VOLUME.
    Delete the OK button.
    Add a new button and name it IDC_BTN_CALCULATE. Set its caption to &Calculate and Check its Default Button check box.
    Change the caption of the Cancel button to E&xit.
  8. To prepare the dialog for coding, right-click the dialog box and click ClassWizard...
  9. click the Member Variables property sheet and change the names of the controls as follows:
     
    ID Type Name
    IDC_EDIT_AREA CString m_Area
    IDC_EDIT_HEIGHT CString m_Height
    IDC_EDIT_LENGTH CString m_Length
    IDC_EDIT_VOLUME CString m_Volume
    IDC_EDIT_WIDTH CString m_Width
  10. Click the Message Maps property sheet. click IDC_BTN_CALCULATE.
  11. In the Messages list, double-click BN_CLICKED, change the name of the function to OnCalculate and click OK.
  12. Click Edit Code.
  13. Press Ctrl + Home to get to the top of the exoMFCUsingDLLDlg.cpp file and just under #endif, include the function we will use from the DLL as follows:
     
    // ExoMFCUsingDLLDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "ExoMFCUsingDLL.h"
    #include "ExoMFCUsingDLLDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    extern "C" __declspec(dllimport)void BoxProperties(double L, double H,
                                        double W, double& A, double& V);
    
    /////////////////////////////////////////////////////////////////////////////
  14. To make the library part of the current project, on the main menu, click Project -> Add To Project -> Files...
  15. On the Insert Files Into Project dialog box, change the Files Of Type combo box to Library Files (*.lib).
  16. In the list of files, click ExoDLL.lib and click OK.
  17. Implement the OnCalculate function as follows:
     
    void CExoMFCUsingDLLDlg::OnCalculate() 
    {
    	// TODO: Add your control notification handler code here
    	double Length, Height, Width, Area, Volume;
    
    	UpdateData(TRUE);
    
    	Length = atof(m_Length);
    	Height = atof(m_Height);
    	Width  = atof(m_Width);
    
    	BoxProperties(Length, Height, Width, Area, Volume);
    
    	m_Area.Format("%.2f", Area);
    	m_Volume.Format("%.2f", Volume);
    
    	UpdateData(FALSE);
    }
  18. On the main menu, click Build -> Set Active Configuration...
  19. Click ExoMFCUsingDLL - Win32 Release and click OK.
  20. To test your program, press Ctrl + F5. VOILA.
 

Previous Copyright © 2002-2004 FunctionX, Inc. Next